<?php


namespace apps\marriage\core\logic;


use apps\marriage\core\service\SiteConfigService;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Faceid\V20180301\FaceidClient;
use TencentCloud\Faceid\V20180301\Models\DetectAuthRequest;
use TencentCloud\Faceid\V20180301\Models\GetDetectInfoRequest;
use think\Exception;
use think\facade\Log;

class AuthLogic
{
    const QCLOUD_BIZTOKEN_CACHE_PREFIX = 'qcloud_faceid_biztoken:';

    private $qcloudSecretId = '';
    private $qcloudSecretKey = '';
    private $RuleId = '2';

    public function __construct()
    {
        $this->qcloudSecretId = $this->getSiteConfigService()->getValueByCode('qcloud_secret_id');
        $this->qcloudSecretKey = $this->getSiteConfigService()->getValueByCode('qcloud_secret_key');
    }

    /**
     * @return \vm\com\BaseService|SiteConfigService
     */
    private function getSiteConfigService()
    {
        return service('SiteConfig', SERVICE_NAMESPACE);
    }

    public function getFaceDetectUrl($userId, $name, $IdCard, $url)
    {
        try {
//            include_once VENDOR_PATH . './guzzlehttp/guzzle/src/functions.php';
//            include_once VENDOR_PATH . './guzzlehttp/psr7/src/functions.php';
//            include_once VENDOR_PATH . './guzzlehttp/promises/src/functions.php';

            $cred = new Credential($this->qcloudSecretId, $this->qcloudSecretKey);
            $client = new FaceidClient($cred, "ap-guangzhou");
            $req = new DetectAuthRequest();
            $req->RuleId = $this->RuleId;
            $req->IdCard = $IdCard;
            $req->Name = $name;
            $req->RedirectUrl = $url;
            $req->Extra = '';
            $req->ImageBase64 = '';

            $resp = $client->DetectAuth($req);
            $data = json_decode($resp->toJsonString(), 1);
            cache(self::QCLOUD_BIZTOKEN_CACHE_PREFIX . $userId, $data['BizToken']);
            return $data['Url'];
        }
        catch(TencentCloudSDKException $e) {
            Log::error('file:' . $e->getFile());
            Log::error('line:' . $e->getLine());
            Log::error('msg:' . $e->getMessage());
            throw new Exception('发生错误');
        }
    }

    public function getFaceIdDetectInfo($userId)
    {
        try {
//            include_once VENDOR_PATH . './guzzlehttp/guzzle/src/functions.php';
//            include_once VENDOR_PATH . './guzzlehttp/psr7/src/functions.php';
//            include_once VENDOR_PATH . './guzzlehttp/promises/src/functions.php';

            $BizToken = cache(self::QCLOUD_BIZTOKEN_CACHE_PREFIX . $userId);

            $cred = new Credential($this->qcloudSecretId, $this->qcloudSecretKey);
            $client = new FaceidClient($cred, "ap-guangzhou");
            $req = new GetDetectInfoRequest();
            $req->RuleId = $this->RuleId;
            $req->BizToken = $BizToken;
            $req->InfoType = '0';

            $resp = $client->GetDetectInfo($req);
            $data = json_decode($resp->toJsonString(), 1);

            return json_decode($data['DetectInfo'], true);
        }
        catch(TencentCloudSDKException $e) {
            Log::error('file:' . $e->getFile());
            Log::error('line:' . $e->getLine());
            Log::error('msg:' . $e->getMessage());
            throw new Exception('发生错误');
        }
    }
}